/*****************************************
 * Copyright (c) 2008 by AvePoint, Inc
 *
 * AvePoint, Inc.
 * 3 Second Street, Suite 803
 * Jersey City, NJ 07311
 * United States of America
 * Telephone: +1-201-793-1111
 * WWW: www.avepoint.com
 */
package com.myassignment.client;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.List;

import com.myassignment.util.Constants;
import com.myassignment.util.DataPacketDto;
import com.myassignment.util.XMLSAXUtility;

public class Client2 implements Constants {
    public static void main(String[] args) {
        try {

            Receive2Handler receiver2 = new Receive2Handler(Client_2_Port_Listen);
            Thread t = new Thread(receiver2);
            t.start();
            // send registration

            DataPacketDto register = new DataPacketDto("Client2", "GateWay1");
            register.setTTL(9);
            register.setDelay(50);
            register.setSourceAdress("127.0.0.1");
            register.setTargetPort(Client_2_Port_Listen);
            register.setType(Request_type_Registration);
            String registerInfo = register.outerXML();
            DatagramPacket register1DataPack = new DatagramPacket(registerInfo.getBytes("UTF-8"),registerInfo.length(),
                    InetAddress.getByName("127.0.0.1"), Gate_Way_1_Port_Listen);
            register.setDstUser("GateWay2");
            register.setDelay(20);
            registerInfo = register.outerXML();
            DatagramPacket register2DataPack = new DatagramPacket(registerInfo.getBytes("UTF-8"),registerInfo.length(),
                    InetAddress.getByName("127.0.0.1"), Gate_Way_2_Port_Listen);
            DatagramSocket client = new DatagramSocket();
            client.send(register1DataPack);
            client.send(register2DataPack);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
class Receive2Handler implements Runnable, Constants {
    private DatagramSocket datagramSocket;
    public Receive2Handler(int port) {
        try {
            this.datagramSocket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        };
    }
    @SuppressWarnings("unchecked")
    @Override
    public void run() {
        try {
            while(true) {
                byte[] buffer = new byte[1024*1024];
                DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
                datagramSocket.receive(datagramPacket);
                String receivedData = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
                if (receivedData.startsWith("<")) {
                    List<DataPacketDto> DataPacketDtos = XMLSAXUtility.getReceivePacketInfos(receivedData);
                    if (DataPacketDtos != null && DataPacketDtos.size() > 0) {
                        StringBuilder info = new StringBuilder();
                        for (DataPacketDto dto : DataPacketDtos) {
                            if (dto.getSrcUser() != null) {
                                info.append(dto.getSrcUser() + "->");
                            }
                        }
                        info.append("Client2");
                        System.out.println(info.toString());
                        continue;
                    }
                }
                System.out.println("Receive packet [IP:" + datagramPacket.getAddress().getHostAddress()+ ",Port:" + datagramPacket.getPort() + "]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}